<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li class="active"><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. Express 介绍">1. Express 介绍</a></li><li><a href="#t12. Express 的应用">2. Express 的应用</a><ul><li><a href="#t22.1 安装express">2.1 安装express</a></li><li><a href="#t32.2 使用express">2.2 使用express</a></li><li><a href="#t42.3 express的get方法">2.3 express的get方法</a></li><li><a href="#t52.4 express的post方法">2.4 express的post方法</a></li><li><a href="#t62.5 express的all方法">2.5 express的all方法</a></li><li><a href="#t72.6 中间件">2.6 中间件</a></li><li><a href="#t82.7 获取参数和查询字符串">2.7 获取参数和查询字符串</a></li><li><a href="#t92.8 获取params参数">2.8 获取params参数</a></li><li><a href="#t102.9 express中的send方法">2.9 express中的send方法</a></li></ul></li><li><a href="#t113. 模板的应用">3. 模板的应用</a><ul><li><a href="#t123.1 安装ejs">3.1 安装ejs</a></li><li><a href="#t133.2 设置模板">3.2 设置模板</a></li><li><a href="#t143.3 渲染html">3.3 渲染html</a></li><li><a href="#t153.4 渲染视图">3.4 渲染视图</a></li><li><a href="#t163.5 模板的实现">3.5 模板的实现</a></li></ul></li><li><a href="#t174. 静态文件服务器">4. 静态文件服务器</a><ul><li><a href="#t184.1 静态文件服务器实现">4.1 静态文件服务器实现</a></li></ul></li><li><a href="#t195. 重定向">5. 重定向</a><ul><li><a href="#t205.1 redirect使用">5.1 redirect使用</a></li><li><a href="#t215.2 redirect的实现">5.2 redirect的实现</a></li></ul></li><li><a href="#t226. 接收 post 响应体">6. 接收 post 响应体</a><ul><li><a href="#t236.1 使用body-parser">6.1 使用body-parser</a></li><li><a href="#t246.2 req.body的实现">6.2 req.body的实现</a></li></ul></li><li><a href="#t25源代码地址">源代码地址</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. Express &#x4ECB;&#x7ECD;">1. Express &#x4ECB;&#x7ECD; <a href="#t01. Express &#x4ECB;&#x7ECD;"> # </a></h2>
<p>Express &#x662F;&#x4E00;&#x4E2A;&#x5C0F;&#x5DE7;&#x4E14;&#x7075;&#x6D3B;&#x7684; Node.js Web&#x5E94;&#x7528;&#x6846;&#x67B6;&#xFF0C;&#x5B83;&#x6709;&#x4E00;&#x5957;&#x5065;&#x58EE;&#x7684;&#x7279;&#x6027;&#xFF0C;&#x53EF;&#x7528;&#x4E8E;&#x5F00;&#x53D1;&#x5355;&#x9875;&#x3001;&#x591A;&#x9875;&#x548C;&#x6DF7;&#x5408;Web&#x5E94;&#x7528;&#x3002;</p>
<p><a href="http://www.expressjs.com.cn/">express</a></p>
<h2 id="t12. Express &#x7684;&#x5E94;&#x7528;">2. Express &#x7684;&#x5E94;&#x7528; <a href="#t12. Express &#x7684;&#x5E94;&#x7528;"> # </a></h2>
<h5 id="t22.1 &#x5B89;&#x88C5;express">2.1 &#x5B89;&#x88C5;express <a href="#t22.1 &#x5B89;&#x88C5;express"> # </a></h5>
<p>npm&#x5B89;&#x88C5;</p>
<pre><code class="lang-shell"><span class="hljs-meta">$</span><span class="bash"> npm install express</span>
</code></pre>
<h5 id="t32.2 &#x4F7F;&#x7528;express">2.2 &#x4F7F;&#x7528;express <a href="#t32.2 &#x4F7F;&#x7528;express"> # </a></h5>
<p>&#x521B;&#x5EFA;http&#x670D;&#x52A1;</p>
<pre><code class="lang-JS"><span class="hljs-comment">//&#x5F15;&#x5165;express</span>
<span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-comment">//&#x6267;&#x884C;express**&#x51FD;&#x6570;</span>
<span class="hljs-keyword">var</span> app = express();
<span class="hljs-comment">//&#x76D1;&#x542C;&#x7AEF;&#x53E3;</span>
app.listen(<span class="hljs-number">3000</span>);
</code></pre>
<h5 id="t42.3 express&#x7684;get&#x65B9;&#x6CD5;">2.3 express&#x7684;get&#x65B9;&#x6CD5; <a href="#t42.3 express&#x7684;get&#x65B9;&#x6CD5;"> # </a></h5>
<p>&#x6839;&#x636E;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x6765;&#x5904;&#x7406;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x51FA;&#x7684;GET&#x8BF7;&#x6C42;</p>
<ul>
<li>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;path&#x4E3A;&#x8BF7;&#x6C42;&#x7684;&#x8DEF;&#x5F84;</li>
<li>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;</li>
</ul>
<pre><code class="lang-js">app.get(path,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>));
</span></code></pre>
<p>get&#x65B9;&#x6CD5;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">const</span> app = express();
app.get(<span class="hljs-string">&apos;/hello&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.end(<span class="hljs-string">&apos;hello&apos;</span>);
});
app.get(<span class="hljs-string">&apos;/world&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.end(<span class="hljs-string">&apos;world&apos;</span>);
});
app.get(<span class="hljs-string">&apos;*&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.setHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>,<span class="hljs-string">&apos;text/plain;charset=utf8&apos;</span>);
    res.end(<span class="hljs-string">&apos;Not Found&apos;</span>);
});
app.listen(<span class="hljs-number">3000</span>);

</code></pre>
<p>get&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>);
<span class="hljs-keyword">let</span> express = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> app = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">let</span> {pathname} = url.parse(req.url, <span class="hljs-literal">true</span>);
        <span class="hljs-keyword">let</span> method = req.method.toLowerCase();
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; app.routes.length; i++) {
            <span class="hljs-keyword">let</span> {path, method, handler} = app.routes[i];
            <span class="hljs-keyword">if</span> ((path == pathname || path == <span class="hljs-string">&quot;*&quot;</span>) &amp;&amp; method == req.method.toLowerCase()) {
                <span class="hljs-keyword">return</span> handler(req, res);
            }
        }
        res.end(<span class="hljs-string">`CANNOT <span class="hljs-subst">${req.method}</span> <span class="hljs-subst">${req.url}</span>`</span>);
    }
    app.routes = [];
    app.listen = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">port</span>) </span>{
        <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>).createServer(app).listen(port);
    }
    app.get = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
        app.routes.push({
            path, handler, <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;get&apos;</span>
        });
    }
    <span class="hljs-keyword">return</span> app;
}

<span class="hljs-built_in">module</span>.exports = express;
</code></pre>
<h5 id="t52.4 express&#x7684;post&#x65B9;&#x6CD5;">2.4 express&#x7684;post&#x65B9;&#x6CD5; <a href="#t52.4 express&#x7684;post&#x65B9;&#x6CD5;"> # </a></h5>
<p>&#x6839;&#x636E;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x6765;&#x5904;&#x7406;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x51FA;&#x7684;POST&#x8BF7;&#x6C42;</p>
<ul>
<li>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;path&#x4E3A;&#x8BF7;&#x6C42;&#x7684;&#x8DEF;&#x5F84;</li>
<li>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;</li>
</ul>
<pre><code class="lang-js">app.post(path,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>));
</span></code></pre>
<p>post&#x65B9;&#x6CD5;&#x7684;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">//&#x5F15;&#x5165;express</span>
<span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./express&apos;</span>);
<span class="hljs-comment">//&#x6267;&#x884C;express&#x51FD;&#x6570;</span>
<span class="hljs-keyword">var</span> app = express();
<span class="hljs-comment">//&#x76D1;&#x542C;&#x7AEF;&#x53E3;</span>
app.post(<span class="hljs-string">&apos;/hello&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
   res.end(<span class="hljs-string">&apos;hello&apos;</span>);
});
app.post(<span class="hljs-string">&apos;*&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.end(<span class="hljs-string">&apos;post&#x6CA1;&#x627E;&#x5230;&apos;</span>);
});
app.listen(<span class="hljs-number">3000</span>);
</code></pre>
<p>&#x901A;&#x8FC7;linux&#x547D;&#x4EE4;&#x53D1;&#x9001;post&#x8BF7;&#x6C42;</p>
<pre><code class="lang-shell"><span class="hljs-meta">$</span><span class="bash"> curl -X POST http://localhost:3000/hello</span>
</code></pre>
<p>post&#x7684;&#x5B9E;&#x73B0;&#xFF1A;</p>
<p>&#x589E;&#x52A0;&#x6240;&#x6709;&#x8BF7;&#x6C42;&#x7684;&#x65B9;&#x6CD5;</p>
<pre><code class="lang-js">http.METHODS.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">method</span>)</span>{
        app[method] = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
            app.routes.push({
                path, handler, method
            });
        }
});
</code></pre>
<h5 id="t62.5 express&#x7684;all&#x65B9;&#x6CD5;">2.5 express&#x7684;all&#x65B9;&#x6CD5; <a href="#t62.5 express&#x7684;all&#x65B9;&#x6CD5;"> # </a></h5>
<p>&#x76D1;&#x542C;&#x6240;&#x6709;&#x7684;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5339;&#x914D;&#x6240;&#x6709;&#x7684;HTTP&#x52A8;&#x8BCD;&#x3002;&#x6839;&#x636E;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x6765;&#x5904;&#x7406;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x51FA;&#x7684;&#x6240;&#x6709;&#x8BF7;&#x6C42;</p>
<ul>
<li>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;path&#x4E3A;&#x8BF7;&#x6C42;&#x7684;&#x8DEF;&#x5F84;</li>
<li>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;</li>
</ul>
<pre><code class="lang-js">app.all(path,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res</span>));
</span></code></pre>
<p>all&#x7684;&#x65B9;&#x6CD5;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">const</span> app = express();
app.all(<span class="hljs-string">&apos;/world&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.end(<span class="hljs-string">&apos;all world&apos;</span>);
});
app.listen(<span class="hljs-number">3000</span>);

</code></pre>
<pre><code class="lang-js">app.all = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
        app.routes.push({
            path, handler, <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;all&apos;</span>
        });
    }
</code></pre>
<h5 id="t72.6 &#x4E2D;&#x95F4;&#x4EF6;">2.6 &#x4E2D;&#x95F4;&#x4EF6; <a href="#t72.6 &#x4E2D;&#x95F4;&#x4EF6;"> # </a></h5>
<p>&#x4E2D;&#x95F4;&#x4EF6;&#x5C31;&#x662F;&#x5904;&#x7406;HTTP&#x8BF7;&#x6C42;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x7528;&#x6765;&#x5B8C;&#x6210;&#x5404;&#x79CD;&#x7279;&#x5B9A;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x6BD4;&#x5982;&#x68C0;&#x67E5;&#x7528;&#x6237;&#x662F;&#x5426;&#x767B;&#x5F55;&#x3001;&#x68C0;&#x6D4B;&#x7528;&#x6237;&#x662F;&#x5426;&#x6709;&#x6743;&#x9650;&#x8BBF;&#x95EE;&#x7B49;&#xFF0C;&#x5B83;&#x7684;&#x7279;&#x70B9;&#x662F;&#xFF1A;</p>
<ul>
<li>&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x5904;&#x7406;&#x5B8C;&#x8BF7;&#x6C42;&#x548C;&#x54CD;&#x5E94;&#x53EF;&#x4EE5;&#x628A;&#x76F8;&#x5E94;&#x6570;&#x636E;&#x518D;&#x4F20;&#x9012;&#x7ED9;&#x4E0B;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;</li>
<li>&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7684;next&#x53C2;&#x6570;,&#x8868;&#x793A;&#x63A5;&#x53D7;&#x5176;&#x4ED6;&#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x8C03;&#x7528;&#xFF0C;&#x51FD;&#x6570;&#x4F53;&#x4E2D;&#x7684;next(),&#x8868;&#x793A;&#x5C06;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x7EE7;&#x7EED;&#x4F20;&#x9012;</li>
<li>&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x8DEF;&#x5F84;&#x6765;&#x533A;&#x5206;&#x8FD4;&#x56DE;&#x6267;&#x884C;&#x4E0D;&#x540C;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;</li>
</ul>
<p>&#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#xFF1A;</p>
<p>&#x589E;&#x52A0;&#x4E2D;&#x95F4;&#x4EF6;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> app = express();
app.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res,next</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5168;&#x90E8;&#x5339;&#x914D;&apos;</span>);
    next();
});
app.use(<span class="hljs-string">&apos;/water&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res,next</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x53EA;&#x5339;&#x914D;/water&apos;</span>);
    next();
});
app.get(<span class="hljs-string">&apos;/water&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.end(<span class="hljs-string">&apos;water&apos;</span>);
});
app.listen(<span class="hljs-number">3000</span>);
</code></pre>
<p>use&#x65B9;&#x6CD5;&#x7684;&#x5B9E;&#x73B0;&#xFF1A;&#x5728;&#x8DEF;&#x7531;&#x6570;&#x7EC4;&#x4E2D;&#x589E;&#x52A0;&#x4E2D;&#x95F4;&#x4EF6;</p>
<pre><code class="lang-js">app.use = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path,fn</span>) </span>{
    <span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> fn !=<span class="hljs-string">&apos;function&apos;</span>){
        fn = path;
        path = <span class="hljs-string">&apos;/&apos;</span>;
    }
    app.routes.push({<span class="hljs-attr">method</span>:<span class="hljs-string">&apos;middle&apos;</span>,<span class="hljs-attr">path</span>:path,<span class="hljs-attr">fn</span>:fn});
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>);
<span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> express = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> app = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">let</span> {pathname} = url.parse(req.url, <span class="hljs-literal">true</span>);
        <span class="hljs-keyword">let</span> method = req.method.toLowerCase();
        <span class="hljs-keyword">let</span> index = <span class="hljs-number">0</span>;

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">next</span>(<span class="hljs-params">err</span>) </span>{
            <span class="hljs-keyword">if</span> (index &gt;= app.routes.length) {
                <span class="hljs-keyword">return</span> res.end(<span class="hljs-string">`CANNOT <span class="hljs-subst">${method}</span> <span class="hljs-subst">${pathname}</span>`</span>);
            }
            <span class="hljs-keyword">let</span> route = app.routes[index++];
            <span class="hljs-keyword">if</span> (route.method == <span class="hljs-string">&apos;middle&apos;</span>) {
                <span class="hljs-keyword">if</span> (route.path == <span class="hljs-string">&apos;/&apos;</span> || pathname.startsWith(route.path + <span class="hljs-string">&apos;/&apos;</span>) || route.path == pathname) {
                    route.handler(req, res, next);
                } <span class="hljs-keyword">else</span> {
                    next();
                }
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-keyword">if</span> ((route.path == pathname || route.path == <span class="hljs-string">&quot;*&quot;</span>) &amp;&amp; (route.method == req.method.toLowerCase()) || method == <span class="hljs-string">&apos;all&apos;</span>) {
                    <span class="hljs-keyword">return</span> route.handler(req, res);
                } <span class="hljs-keyword">else</span> {
                    next();
                }
            }
        }

        next();
    }
    app.routes = [];
    app.listen = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">port</span>) </span>{
        http.createServer(app).listen(port);
    }
    http.METHODS.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">method</span>) </span>{
        method = method.toLowerCase();
        app[method] = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
            app.routes.push({
                path, handler, method
            });
        }
    });
    app.all = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
        app.routes.push({
            path, handler, <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;all&apos;</span>
        });
    }
    app.use = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> handler != <span class="hljs-string">&apos;function&apos;</span>) {
            handler = path;
            path = <span class="hljs-string">&quot;/&quot;</span>;
        }
        app.routes.push({
            <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;middle&apos;</span>,
            path,
            handler
        });
    }

    <span class="hljs-keyword">return</span> app;
}

<span class="hljs-built_in">module</span>.exports = express;
</code></pre>
<p>&#x9519;&#x8BEF;&#x4E2D;&#x95F4;&#x4EF6;&#xFF1A;next&#x4E2D;&#x53EF;&#x4EE5;&#x4F20;&#x9012;&#x9519;&#x8BEF;&#xFF0C;&#x9ED8;&#x8BA4;&#x6267;&#x884C;&#x9519;&#x8BEF;&#x4E2D;&#x95F4;&#x4EF6;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> app = express();
app.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res,next</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FC7;&#x6EE4;&#x77F3;&#x5934;&apos;</span>);
    next(<span class="hljs-string">&apos;stone is too big&apos;</span>);
});
app.use(<span class="hljs-string">&apos;/water&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res,next</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FC7;&#x6EE4;&#x6C99;&#x5B50;&apos;</span>);
    next();
});
app.get(<span class="hljs-string">&apos;/water&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.end(<span class="hljs-string">&apos;water&apos;</span>);
});
app.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err,req,res,next</span>) </span>{
    <span class="hljs-built_in">console</span>.log(err);
    res.end(err);
});
app.listen(<span class="hljs-number">3000</span>);
</code></pre>
<p>&#x9519;&#x8BEF;&#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x5B9E;&#x73B0;&#xFF1A;&#x5BF9;&#x9519;&#x8BEF;&#x4E2D;&#x95F4;&#x4EF6;&#x8FDB;&#x884C;&#x5904;&#x7406;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>);
<span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> express = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> app = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">let</span> {pathname} = url.parse(req.url, <span class="hljs-literal">true</span>);
        <span class="hljs-keyword">let</span> method = req.method.toLowerCase();
        <span class="hljs-keyword">let</span> index = <span class="hljs-number">0</span>;

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">next</span>(<span class="hljs-params">err</span>) </span>{
            <span class="hljs-keyword">if</span> (index &gt;= app.routes.length) {
                <span class="hljs-keyword">return</span> res.end(<span class="hljs-string">`CANNOT <span class="hljs-subst">${method}</span> <span class="hljs-subst">${pathname}</span>`</span>);
            }
            <span class="hljs-keyword">let</span> route = app.routes[index++];
            <span class="hljs-keyword">if</span> (err) {
                <span class="hljs-keyword">if</span> (route.method == <span class="hljs-string">&apos;middle&apos;</span> &amp;&amp; route.handler.length == <span class="hljs-number">4</span>) {
                    route.handler(req, res, next)
                }
            }
            <span class="hljs-keyword">if</span> (route.method == <span class="hljs-string">&apos;middle&apos;</span>) {
                <span class="hljs-keyword">if</span> (route.path == <span class="hljs-string">&apos;/&apos;</span> || pathname.startsWith(route.path + <span class="hljs-string">&apos;/&apos;</span>) || route.path == pathname) {
                    route.handler(req, res, next);
                } <span class="hljs-keyword">else</span> {
                    next();
                }
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-keyword">if</span> ((route.path == pathname || route.path == <span class="hljs-string">&quot;*&quot;</span>) &amp;&amp; (route.method == req.method.toLowerCase()) || method == <span class="hljs-string">&apos;all&apos;</span>) {
                    <span class="hljs-keyword">return</span> route.handler(req, res);
                } <span class="hljs-keyword">else</span> {
                    next();
                }
            }
        }

        next();
    }
    app.routes = [];
    app.listen = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">port</span>) </span>{
        http.createServer(app).listen(port);
    }
    http.METHODS.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">method</span>) </span>{
        method = method.toLowerCase();
        app[method] = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
            app.routes.push({
                path, handler, method
            });
        }
    });
    app.all = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
        app.routes.push({
            path, handler, <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;all&apos;</span>
        });
    }
    app.use = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> handler != <span class="hljs-string">&apos;function&apos;</span>) {
            handler = path;
            path = <span class="hljs-string">&quot;/&quot;</span>;
        }
        app.routes.push({
            <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;middle&apos;</span>,
            path,
            handler
        });
    }

    <span class="hljs-keyword">return</span> app;
}

<span class="hljs-built_in">module</span>.exports = express;
</code></pre>
<h5 id="t82.7 &#x83B7;&#x53D6;&#x53C2;&#x6570;&#x548C;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;">2.7 &#x83B7;&#x53D6;&#x53C2;&#x6570;&#x548C;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32; <a href="#t82.7 &#x83B7;&#x53D6;&#x53C2;&#x6570;&#x548C;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;"> # </a></h5>
<ul>
<li><code>req.hostname</code> &#x8FD4;&#x56DE;&#x8BF7;&#x6C42;&#x5934;&#x91CC;&#x53D6;&#x7684;&#x4E3B;&#x673A;&#x540D;</li>
<li><code>req.path</code> &#x8FD4;&#x56DE;&#x8BF7;&#x6C42;&#x7684;URL&#x7684;&#x8DEF;&#x5F84;&#x540D;</li>
<li><code>req.query</code> &#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;</li>
</ul>
<pre><code class="lang-js"><span class="hljs-comment">//http://localhost:3000/?name=zfpx&amp;age=8</span>
app.get(<span class="hljs-string">&apos;/&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.write(<span class="hljs-built_in">JSON</span>.stringify(req.query))
    res.end(req.path+<span class="hljs-string">&quot; &quot;</span>+req.path);
});
</code></pre>
<p>&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#xFF1A;&#x5BF9;&#x8BF7;&#x6C42;&#x589E;&#x52A0;&#x65B9;&#x6CD5;</p>
<pre><code class="lang-js">  req.path = pathname;
  req.query = query;
</code></pre>
<h5 id="t92.8 &#x83B7;&#x53D6;params&#x53C2;&#x6570;">2.8 &#x83B7;&#x53D6;params&#x53C2;&#x6570; <a href="#t92.8 &#x83B7;&#x53D6;params&#x53C2;&#x6570;"> # </a></h5>
<p>req.params &#x5339;&#x914D;&#x5230;&#x7684;&#x6240;&#x6709;&#x8DEF;&#x5F84;&#x53C2;&#x6570;&#x7EC4;&#x6210;&#x7684;&#x5BF9;&#x8C61;</p>
<pre><code class="lang-js">app.get(<span class="hljs-string">&apos;/school/:name/:age&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-built_in">console</span>.log(req.params);
    res.end(<span class="hljs-string">&apos;water&apos;</span>);
});
</code></pre>
<p>params&#x5B9E;&#x73B0;&#xFF1A;&#x589E;&#x52A0;params&#x5C5E;&#x6027;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>);
<span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> express = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> app = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
        <span class="hljs-keyword">let</span> {pathname, query} = url.parse(req.url, <span class="hljs-literal">true</span>);
        <span class="hljs-keyword">let</span> method = req.method.toLowerCase();
        <span class="hljs-keyword">let</span> index = <span class="hljs-number">0</span>;
        req.path = pathname;
        req.query = query;

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">next</span>(<span class="hljs-params">err</span>) </span>{
            <span class="hljs-keyword">if</span> (index &gt;= app.routes.length) {
                <span class="hljs-keyword">return</span> res.end(<span class="hljs-string">`CANNOT <span class="hljs-subst">${method}</span> <span class="hljs-subst">${pathname}</span>`</span>);
            }
            <span class="hljs-keyword">let</span> route = app.routes[index++];

            <span class="hljs-keyword">if</span> (err) {
                <span class="hljs-keyword">if</span> (route.method == <span class="hljs-string">&apos;middle&apos;</span> &amp;&amp; route.handler.length == <span class="hljs-number">4</span>) {
                    route.handler(req, res, next)
                } <span class="hljs-keyword">else</span> {
                    next();
                }
            } <span class="hljs-keyword">else</span> {

                <span class="hljs-keyword">if</span> (route.method == <span class="hljs-string">&apos;middle&apos;</span>) {
                    <span class="hljs-keyword">if</span> (route.path == <span class="hljs-string">&apos;/&apos;</span> || pathname.startsWith(route.path + <span class="hljs-string">&apos;/&apos;</span>) || route.path == pathname) {
                        route.handler(req, res, next);
                    } <span class="hljs-keyword">else</span> {
                        next();
                    }
                } <span class="hljs-keyword">else</span> {
                    <span class="hljs-keyword">if</span> (route.paramNames) {
                        <span class="hljs-keyword">let</span> matchers = pathname.match(<span class="hljs-keyword">new</span> <span class="hljs-built_in">RegExp</span>(route.path));
                        <span class="hljs-keyword">if</span> (matchers) {
                            <span class="hljs-keyword">let</span> params = {};
                            <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;route.paramNames.length;i++){
                                params[route.paramNames[i]] = matchers[i+<span class="hljs-number">1</span>];
                            }
                            req.params = params;
                            route.handler(req,res);
                        }<span class="hljs-keyword">else</span>{
                            next();
                        }
                    } <span class="hljs-keyword">else</span> {
                        <span class="hljs-keyword">if</span> ((route.path == pathname || route.path == <span class="hljs-string">&quot;*&quot;</span>) &amp;&amp; (route.method == req.method.toLowerCase()) || method == <span class="hljs-string">&apos;all&apos;</span>) {
                            <span class="hljs-keyword">return</span> route.handler(req, res);
                        } <span class="hljs-keyword">else</span> {
                            next();
                        }
                    }
                }
            }
        }

        next();
    }
    app.routes = [];
    app.listen = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">port</span>) </span>{
        http.createServer(app).listen(port);
    }
    http.METHODS.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">method</span>) </span>{
        method = method.toLowerCase();
        app[method] = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
            <span class="hljs-keyword">const</span> layer = {path, handler, method};
            <span class="hljs-keyword">if</span> (path.includes(<span class="hljs-string">&apos;:&apos;</span>)) {
                <span class="hljs-keyword">let</span> paramNames = [];
                layer.path = path.replace(<span class="hljs-regexp">/:([^\/]+)/g</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
                    paramNames.push(<span class="hljs-built_in">arguments</span>[<span class="hljs-number">1</span>]);
                    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;([^\/]+)&apos;</span>;
                });
                layer.paramNames = paramNames;
            }
            app.routes.push(layer);
        }
    });
    app.all = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
        app.routes.push({
            path, handler, <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;all&apos;</span>
        });
    }
    app.use = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">path, handler</span>) </span>{
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> handler != <span class="hljs-string">&apos;function&apos;</span>) {
            handler = path;
            path = <span class="hljs-string">&quot;/&quot;</span>;
        }
        app.routes.push({
            <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;middle&apos;</span>,
            path,
            handler
        });
    }
    <span class="hljs-keyword">return</span> app;
}

<span class="hljs-built_in">module</span>.exports = express;
</code></pre>
<h5 id="t102.9 express&#x4E2D;&#x7684;send&#x65B9;&#x6CD5;">2.9 express&#x4E2D;&#x7684;send&#x65B9;&#x6CD5; <a href="#t102.9 express&#x4E2D;&#x7684;send&#x65B9;&#x6CD5;"> # </a></h5>
<p>&#x53C2;&#x6570;&#x4E3A;&#x8981;&#x54CD;&#x5E94;&#x7684;&#x5185;&#x5BB9;,&#x53EF;&#x4EE5;&#x667A;&#x80FD;&#x5904;&#x7406;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;,&#x5728;&#x8F93;&#x51FA;&#x54CD;&#x5E94;&#x65F6;&#x4F1A;&#x81EA;&#x52A8;&#x8FDB;&#x884C;&#x4E00;&#x4E9B;&#x8BBE;&#x7F6E;&#xFF0C;&#x6BD4;&#x5982;HEAD&#x4FE1;&#x606F;&#x3001;HTTP&#x7F13;&#x5B58;&#x652F;&#x6301;&#x7B49;&#x7B49;</p>
<pre><code class="lang-js">res.send([body]);
</code></pre>
<p>&#x5F53;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x65F6;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4F1A;&#x8BBE;&#x7F6E;Content-type&#x4E3A;text/html</p>
<pre><code class="lang-js">app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.send(<span class="hljs-string">&apos;&lt;p&gt;hello world&lt;/p&gt;&apos;</span>);
});
</code></pre>
<p>&#x5F53;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;Array&#x6216;&#x8005;Object&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;json&#x683C;&#x5F0F;</p>
<pre><code class="lang-js">app.get(<span class="hljs-string">&apos;/json&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
     res.send({<span class="hljs-attr">obj</span>:<span class="hljs-number">1</span>});
});
app.get(<span class="hljs-string">&apos;/arr&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
 res.send([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]);
});
</code></pre>
<p>&#x5F53;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;number&#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x5BF9;&#x5E94;&#x7684;&#x72B6;&#x6001;&#x7801;&#x77ED;&#x8BED;</p>
<pre><code class="lang-js">app.get(<span class="hljs-string">&apos;/status&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.send(<span class="hljs-number">404</span>); <span class="hljs-comment">//not found</span>
    <span class="hljs-comment">//res.status(404).send(&apos;&#x6CA1;&#x6709;&#x627E;&#x5230;&apos;);&#x8BBE;&#x7F6E;&#x77ED;&#x8BED;</span>
});
</code></pre>
<p>send&#x65B9;&#x6CD5;&#x7684;&#x5B9E;&#x73B0;&#xFF1A;&#x81EA;&#x5B9A;&#x4E49;send&#x65B9;&#x6CD5;</p>
<pre><code class="lang-js">   res.send = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">msg</span>)</span>{
            <span class="hljs-keyword">let</span> type = <span class="hljs-keyword">typeof</span> msg;
            <span class="hljs-keyword">if</span>(type == <span class="hljs-string">&apos;object&apos;</span>){
                res.setHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>,<span class="hljs-string">&apos;application/json&apos;</span>);
                msg = <span class="hljs-built_in">JSON</span>.stringify(msg);
            }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(type == <span class="hljs-string">&apos;number&apos;</span>){
                res.setHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>,<span class="hljs-string">&apos;application/plain&apos;</span>);
                res.status(msg);
                res.end(http.STATUS_CODES[msg]);
            }<span class="hljs-keyword">else</span>{
                res.setHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>,<span class="hljs-string">&apos;application/html&apos;</span>);
                res.end(msg);
            }
        }
</code></pre>
<h2 id="t113. &#x6A21;&#x677F;&#x7684;&#x5E94;&#x7528;">3. &#x6A21;&#x677F;&#x7684;&#x5E94;&#x7528; <a href="#t113. &#x6A21;&#x677F;&#x7684;&#x5E94;&#x7528;"> # </a></h2>
<h5 id="t123.1 &#x5B89;&#x88C5;ejs">3.1 &#x5B89;&#x88C5;ejs <a href="#t123.1 &#x5B89;&#x88C5;ejs"> # </a></h5>
<p>npm&#x5B89;&#x88C5;ejs</p>
<pre><code class="lang-shell"><span class="hljs-meta">$</span><span class="bash"> npm install ejs</span>
</code></pre>
<h5 id="t133.2 &#x8BBE;&#x7F6E;&#x6A21;&#x677F;">3.2 &#x8BBE;&#x7F6E;&#x6A21;&#x677F; <a href="#t133.2 &#x8BBE;&#x7F6E;&#x6A21;&#x677F;"> # </a></h5>
<p>&#x4F7F;&#x7528;ejs&#x6A21;&#x7248;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">var</span> app = express();
app.set(<span class="hljs-string">&apos;view engine&apos;</span>,<span class="hljs-string">&apos;ejs&apos;</span>);
app.set(<span class="hljs-string">&apos;views&apos;</span>,path.join(__dirname,<span class="hljs-string">&apos;views&apos;</span>));
app.listen(<span class="hljs-number">3000</span>);
</code></pre>
<h5 id="t143.3 &#x6E32;&#x67D3;html">3.3 &#x6E32;&#x67D3;html <a href="#t143.3 &#x6E32;&#x67D3;html"> # </a></h5>
<p>&#x914D;&#x7F6E;&#x6210;html&#x683C;&#x5F0F;</p>
<pre><code class="lang-js">app.set(<span class="hljs-string">&apos;view engine&apos;</span>,<span class="hljs-string">&apos;html&apos;</span>)
app.set(<span class="hljs-string">&apos;views&apos;</span>,path.join(__dirname,<span class="hljs-string">&apos;views&apos;</span>));
app.engine(<span class="hljs-string">&apos;html&apos;</span>,<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;ejs&apos;</span>).__express);
</code></pre>
<h5 id="t153.4 &#x6E32;&#x67D3;&#x89C6;&#x56FE;">3.4 &#x6E32;&#x67D3;&#x89C6;&#x56FE; <a href="#t153.4 &#x6E32;&#x67D3;&#x89C6;&#x56FE;"> # </a></h5>
<ul>
<li>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570; &#x8981;&#x6E32;&#x67D3;&#x7684;&#x6A21;&#x677F;</li>
<li>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570; &#x6E32;&#x67D3;&#x6240;&#x9700;&#x8981;&#x7684;&#x6570;&#x636E;</li>
</ul>
<pre><code class="lang-js">app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.render(<span class="hljs-string">&apos;hello&apos;</span>,{<span class="hljs-attr">title</span>:<span class="hljs-string">&apos;hello&apos;</span>},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,data</span>)</span>{});
});
</code></pre>
<h5 id="t163.5 &#x6A21;&#x677F;&#x7684;&#x5B9E;&#x73B0;">3.5 &#x6A21;&#x677F;&#x7684;&#x5B9E;&#x73B0; <a href="#t163.5 &#x6A21;&#x677F;&#x7684;&#x5B9E;&#x73B0;"> # </a></h5>
<p>&#x8BFB;&#x53D6;&#x6A21;&#x7248;&#x6E32;&#x67D3;</p>
<pre><code class="lang-js">res.render = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">name, data</span>) </span>{
    <span class="hljs-keyword">var</span> viewEngine = engine.viewEngineList[engine.viewType];
    <span class="hljs-keyword">if</span> (viewEngine) {
        viewEngine(path.join(engine.viewsPath, name + <span class="hljs-string">&apos;.&apos;</span> + engine.viewType), data, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, data</span>) </span>{
            <span class="hljs-keyword">if</span> (err) {
                res.status(<span class="hljs-number">500</span>).sendHeader().send(<span class="hljs-string">&apos;view engine failure&apos;</span> + err);
            } <span class="hljs-keyword">else</span> {
                res.status(<span class="hljs-number">200</span>).contentType(<span class="hljs-string">&apos;text/html&apos;</span>).sendHeader().send(data);
            }
        });
    } <span class="hljs-keyword">else</span> {
        res.status(<span class="hljs-number">500</span>).sendHeader().send(<span class="hljs-string">&apos;view engine failure&apos;</span>);
    }
}
</code></pre>
<h2 id="t174. &#x9759;&#x6001;&#x6587;&#x4EF6;&#x670D;&#x52A1;&#x5668;">4. &#x9759;&#x6001;&#x6587;&#x4EF6;&#x670D;&#x52A1;&#x5668; <a href="#t174. &#x9759;&#x6001;&#x6587;&#x4EF6;&#x670D;&#x52A1;&#x5668;"> # </a></h2>
<p>&#x5982;&#x679C;&#x8981;&#x5728;&#x7F51;&#x9875;&#x4E2D;&#x52A0;&#x8F7D;&#x9759;&#x6001;&#x6587;&#x4EF6;&#xFF08;css&#x3001;js&#x3001;img&#xFF09;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x53E6;&#x5916;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x5B58;&#x653E;&#x9759;&#x6001;&#x6587;&#x4EF6;&#x7684;&#x76EE;&#x5F55;&#xFF0C;&#x5F53;&#x6D4F;&#x89C8;&#x5668;&#x53D1;&#x51FA;&#x975E;HTML&#x6587;&#x4EF6;&#x8BF7;&#x6C42;&#x65F6;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5C31;&#x4F1A;&#x5230;&#x8FD9;&#x4E2A;&#x76EE;&#x5F55;&#x4E0B;&#x53BB;&#x5BFB;&#x627E;&#x76F8;&#x5173;&#x6587;&#x4EF6;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> app = express();
<span class="hljs-keyword">var</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
app.use(express.static(path.join(__dirname,<span class="hljs-string">&apos;public&apos;</span>)));
app.listen(<span class="hljs-number">3000</span>);
</code></pre>
<h5 id="t184.1 &#x9759;&#x6001;&#x6587;&#x4EF6;&#x670D;&#x52A1;&#x5668;&#x5B9E;&#x73B0;">4.1 &#x9759;&#x6001;&#x6587;&#x4EF6;&#x670D;&#x52A1;&#x5668;&#x5B9E;&#x73B0; <a href="#t184.1 &#x9759;&#x6001;&#x6587;&#x4EF6;&#x670D;&#x52A1;&#x5668;&#x5B9E;&#x73B0;"> # </a></h5>
<p>&#x914D;&#x7F6E;&#x9759;&#x6001;&#x670D;&#x52A1;&#x5668;</p>
<pre><code class="lang-js">express.static = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">p</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
        <span class="hljs-keyword">var</span> staticPath = path.join(p, req.path);
        <span class="hljs-keyword">var</span> exists = fs.existsSync(staticPath);
        <span class="hljs-keyword">if</span> (exists) {
            res.sendFile(staticPath);
        } <span class="hljs-keyword">else</span> {
            next();
        }
    }
};

</code></pre>
<h2 id="t195. &#x91CD;&#x5B9A;&#x5411;">5. &#x91CD;&#x5B9A;&#x5411; <a href="#t195. &#x91CD;&#x5B9A;&#x5411;"> # </a></h2>
<p>redirect&#x65B9;&#x6CD5;&#x5141;&#x8BB8;&#x7F51;&#x5740;&#x7684;&#x91CD;&#x5B9A;&#x5411;&#xFF0C;&#x8DF3;&#x8F6C;&#x5230;&#x6307;&#x5B9A;&#x7684;url&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;status&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;302&#x65B9;&#x5F0F;&#x3002;</p>
<ul>
<li>&#x53C2;&#x6570;1 &#x72B6;&#x6001;&#x7801;(&#x53EF;&#x9009;)</li>
<li>&#x53C2;&#x6570;2 &#x8DF3;&#x8F6C;&#x7684;&#x8DEF;&#x5F84;</li>
</ul>
<pre><code class="lang-js">res.redirect([status], url);
</code></pre>
<h5 id="t205.1 redirect&#x4F7F;&#x7528;">5.1 redirect&#x4F7F;&#x7528; <a href="#t205.1 redirect&#x4F7F;&#x7528;"> # </a></h5>
<p>&#x4F7F;&#x7528;&#x91CD;&#x5B9A;&#x5411;</p>
<pre><code class="lang-js">app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.redirect(<span class="hljs-string">&apos;http://www.baidu.com&apos;</span>)
});
</code></pre>
<h5 id="t215.2 redirect&#x7684;&#x5B9E;&#x73B0;">5.2 redirect&#x7684;&#x5B9E;&#x73B0; <a href="#t215.2 redirect&#x7684;&#x5B9E;&#x73B0;"> # </a></h5>
<p>302&#x91CD;&#x5B9A;&#x5411;</p>
<pre><code class="lang-js">res.redirect = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">url</span>) </span>{
    res.status(<span class="hljs-number">302</span>);
    res.headers(<span class="hljs-string">&apos;Location&apos;</span>, url || <span class="hljs-string">&apos;/&apos;</span>);
    res.sendHeader();
    res.end();
};
</code></pre>
<h2 id="t226. &#x63A5;&#x6536; post &#x54CD;&#x5E94;&#x4F53;">6. &#x63A5;&#x6536; post &#x54CD;&#x5E94;&#x4F53; <a href="#t226. &#x63A5;&#x6536; post &#x54CD;&#x5E94;&#x4F53;"> # </a></h2>
<p>&#x5B89;&#x88C5;body-parser</p>
<pre><code class="lang-shell"><span class="hljs-meta">$</span><span class="bash"> npm install body-parser</span>
</code></pre>
<h5 id="t236.1 &#x4F7F;&#x7528;body-parser">6.1 &#x4F7F;&#x7528;body-parser <a href="#t236.1 &#x4F7F;&#x7528;body-parser"> # </a></h5>
<p>&#x63A5;&#x6536;&#x8BF7;&#x6C42;&#x4F53;&#x4E2D;&#x7684;&#x6570;&#x636E;</p>
<pre><code class="lang-js">app.get(<span class="hljs-string">&apos;/login&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.sendFile(<span class="hljs-string">&apos;./login.html&apos;</span>,{<span class="hljs-attr">root</span>:__dirname})
});
app.post(<span class="hljs-string">&apos;/user&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    <span class="hljs-built_in">console</span>.log(req.body);
    res.send(req.body);
});
app.listen(<span class="hljs-number">3000</span>);
</code></pre>
<h5 id="t246.2 req.body&#x7684;&#x5B9E;&#x73B0;">6.2 req.body&#x7684;&#x5B9E;&#x73B0; <a href="#t246.2 req.body&#x7684;&#x5B9E;&#x73B0;"> # </a></h5>
<p>&#x5B9E;&#x73B0;bodyParser</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">bodyParser</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res,next</span>) </span>{
        <span class="hljs-keyword">var</span> result = <span class="hljs-string">&apos;&apos;</span>;
        req.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
            result+=data;
        });
        req.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">try</span>{
                req.body = <span class="hljs-built_in">JSON</span>.parse(result);
            }<span class="hljs-keyword">catch</span>(e){
                req.body = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;querystring&apos;</span>).parse(result);
            }
            next();
        })
    }
};
</code></pre>
<h2 id="t25&#x6E90;&#x4EE3;&#x7801;&#x5730;&#x5740;">&#x6E90;&#x4EE3;&#x7801;&#x5730;&#x5740; <a href="#t25&#x6E90;&#x4EE3;&#x7801;&#x5730;&#x5740;"> # </a></h2>
<p><a href="https://github.com/YataoZhang/mockxpress">https://github.com/YataoZhang/mockxpress</a></p>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
